package com.system.controller;

import com.system.domain.Cart;
import com.system.domain.Phones;
import com.system.domain.User;
import com.system.service.Impl.PhoneServiceImpl;
import com.system.service.PhoneService;
import org.apache.shiro.web.subject.support.WebDelegatingSubject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class mainController {

    @Resource
    private PhoneServiceImpl phoneServiceImpl;

    //获取热销
    @RequestMapping("/getShop_hot")
    public String getShop_List(Model model){
        List<Phones> phones = phoneServiceImpl.getPhonesById();
        model.addAttribute("phones",phones);
        return "shop_hot";
    }
    //添加购物车
    @RequestMapping("/add_card")
    @ResponseBody
    public Integer addToCart(Integer pid, HttpSession session){
        //获取用户的id
        WebDelegatingSubject webDelegatingSubject = (WebDelegatingSubject) session.getAttribute("user");
        User user = (User) webDelegatingSubject.getPrincipal();
        int uid = user.getId();
//        int uid=((User)session.getAttribute("user")).getId();
        ModelAndView modelAndView=new ModelAndView("shop_list");
        int phonesCart = phoneServiceImpl.selectCartCount(pid,uid);
        if (phonesCart > 0) {
//           modelAndView.addObject("result", 2);
            System.out.println("购物车已经添加过了");
            return 2;
        }else {
            phoneServiceImpl.addCart(pid,uid);
            System.out.println("加入购物车成功");
//            modelAndView.addObject("result", 1);
            return 1;
        }
//        return  modelAndView;


        //更新购物车中的数据


    }

    //查询购物车
    @RequestMapping("/selectCart")
    public String selectCart(Model model){
        List<Phones> phonesInfo = phoneServiceImpl.selectCart();
        int num=0;
        for (Phones phones : phonesInfo) {
            num+=1;
            model.addAttribute("num",num);
        }
        model.addAttribute("phonesInfo",phonesInfo);
        return "cart_list";
    }


    //删除单个购物车
    @RequestMapping("/del_card")
    @ResponseBody
    public Integer delToCart(Integer pid, HttpSession session){
        //获取手机的id 查询购物车id
        int cid = getCardCid(pid,session);


        int phonesCart = phoneServiceImpl.delToCart(cid);
        return phonesCart;
    }

    private Integer getCardCid(Integer pid ,HttpSession session) {
        WebDelegatingSubject webDelegatingSubject = (WebDelegatingSubject) session.getAttribute("user");
        User user = (User) webDelegatingSubject.getPrincipal();
        int uid = user.getId();

        int cid = phoneServiceImpl.selectCartCid(pid,uid);
        return cid;
    }

    @RequestMapping("/del_list_cart")
    @ResponseBody
    public Integer delToCarts(String ids,HttpSession session){
        //获取用户的id
        WebDelegatingSubject webDelegatingSubject = (WebDelegatingSubject) session.getAttribute("user");
        User user = (User) webDelegatingSubject.getPrincipal();
        int uid = user.getId();

        List<Integer> cardIds = phoneServiceImpl.selectCartCids(ids,uid);
        String[] cards = new String[cardIds.size()];
        for (int i =0 ; i < cardIds.size(); i ++) {
            cards[i] = String.valueOf(cardIds.get(i));

        }

        int phonesCart = phoneServiceImpl.delToCarts(cards);


        return phonesCart;
    }

    //分页功能
    @RequestMapping("/findByPage")
    public String findByPage(
            Cart cart,
            @RequestParam(value = "pageCode",required = false,defaultValue = "1") int pageCode,
            @RequestParam(value = "pageSize",required = false,defaultValue = "1") int pageSize,
            Model model){

        System.out.println("findByPage.......");
        //回显数据
        model.addAttribute("page",phoneServiceImpl.findByPage(cart,pageCode,pageSize));
        return "cart_list";
    }


}
